package com.imooc.controller;

import com.imooc.converter.CategoryForm2ProductCategoryConverter;
import com.imooc.dataobject.ProductCategory;
import com.imooc.enums.ResultEnum;
import com.imooc.form.CategoryForm;
import com.imooc.form.group.Insert;
import com.imooc.form.group.Update;
import com.imooc.service.CategoryService;
import com.imooc.utils.ValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * FileName: SellerCategoryController.java
 * Description: 卖家类目controller
 *
 * @author HuangRI(874668739 @ qq.com)
 * @version 1.0
 * @date 2020/12/31
 */
@Controller
@Slf4j
@RequestMapping("/seller/category")
public class SellerCategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ModelAndView list(/*@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "#{sellerConfig.page.size}") Integer size,*/
                            Map<String, Object> model) {
        List<ProductCategory> categories;
        try {
            categories = this.categoryService.findAll();
        } catch (Exception e) {
            log.error("【卖家端查询类目列表】发生异常", e);
            categories = new ArrayList<>();
        }

        model.put("categories", categories);
        return new ModelAndView("/category/list", model);
    }

    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "categoryId", required = false) Integer categoryId,
                              Map<String, Object> model) {
        try {
            ProductCategory category;
            if (null != categoryId) {
                category = this.categoryService.findOne(categoryId);
                if (category == null) {
                    log.error("【卖家编辑类目】类目不存在，categoryId={}", categoryId);
                    return new ModelAndView("redirect:/sell/seller/category/list");
                }
            } else {
                category = new ProductCategory();
            }

            model.put("category", category);
            return new ModelAndView("/category/index", model);
        } catch (Exception e) {
            log.error("【卖家编辑类目】查询类目出错，categoryId={}", categoryId);
            return new ModelAndView("redirect:/sell/seller/category/list");
        }
    }

    @PostMapping("/save")
    public ModelAndView save(CategoryForm categoryForm,
                             Map<String, Object> model) {
        String failUrl = "/sell/seller/category/index";
        String successUrl = "/sell/seller/category/list";
        if (null != categoryForm.getCategoryId()) {
            failUrl += "?categoryId=" + categoryForm.getCategoryId();
            successUrl = failUrl;
        }

        try {
            if (null != categoryForm.getCategoryId()) {
                ValidateUtil.validate(categoryForm, Update.class);
            } else {
                ValidateUtil.validate(categoryForm, Insert.class);
            }

            this.categoryService.save(CategoryForm2ProductCategoryConverter.convert(categoryForm));
        } catch (Exception e) {
            log.error("【买家端保存类目】发生异常", e);
            model.put("msg", e.getMessage());
            model.put("url", failUrl);
            return new ModelAndView("/common/error", model);
        }

        model.put("msg", ResultEnum.CATEGORY_SAVE_SUCCESS.getMessage());
        model.put("url", successUrl);
        return new ModelAndView("/common/success", model);
    }
}
